Teaching Your Person Objects How to Behave

In object-oriented programming terms*904*, teaching your people how to behave is done by adding methods*710* to the Person class. 

Before an object*762* can start behaving, it must exist. One of the first methods typically written for a new class includes operations that create a new instance of the class. What that instance (or object) can then do depends on other methods in the class.

The first method in Person, conception, prompts the end user for a name to give a person who is about to be “born”. conception passes this name character string to a birth method, which creates an instance of class Person, and then sets the new object’s name attribute to the character string passed to birth from conception. Once born in birth, execution continues in conception, where the new Person announces him- or herself by a call to the universal method greetings.

Start with conception:

u Double-click on the right side of the class icon to open the Methods window*726* of the Person class. The window opens and the title bar shows a method icon followed by the name of the class.

u Command-click again anywhere in the Person Methods window to create a new method icon.

u Type conception and double-click on the icon of this new method to open its case window.

u Command-click in the middle of the conception case window to add an unnamed Prograph operation icon.

u To open the ˙Info… dialog or bring it to the front, select ˙Info… from the Info menu. Make sure Primitives is selected in the Category pop-up menu, scroll to the ask *835*primitive *80*, and click this list entry. The text panel in the dialog describes the function of this user interaction operation.

u Select Name from the Transfer pop-up menu to paste ask into the generic operation. Press Return. A single-root *20*output *956*appears, since the prompt and default value inputs to ask are optional.

u Place the crosshair cursor just above the upper edge of the ask operation icon and Command-click once. A highlighted *23**1066*input terminal appears along the upper edge of ask. 

===========================================================
*1081*TIP: There are click “target regions” for adding roots and  terminals along the top and bottom edges of an operation icon. Command-clicking too far from the edge of an operation icon adds a new icon. Use the Command-D key combination, or select*1117* Delete Object from the Edit menu, to remove an unwanted icon. If you accidentally delete an operation icon, use Command-Z, or select*606* Undo Delete *288*from the Edit menu, to undo the deletion. 
===========================================================

u Double-click the new terminal on the ask operation to create a NULL-valued input constant.

u Type What is the name of the person about to be born?. You see the entire character string as you type this constant value.*1018* Press Return, and the string is truncated with an ellipsis. 
*314*(Note: The entire string*291* is maintained and can be edited in a dialog by double-clicking the left or right edge of the truncated display.)

Now when conception is executed, a simple dialog prompts for the name of the new person about to be born. When you enter a name and press Return, the name entered is passed out the root of the ask operation, ready to flow through a datalink *251*to wherever that value is needed.

u To create a birth method and pass to it the name string from the root of ask, select the root of the ask operation (that is, click on the root and let go). Then press the Option key and move the cursor (without pressing the mouse button!) to slightly below the ask operation. A datalink stretches out like a rubber band.

===========================================================
*1092*TIP: If you click on the root and accidentally hold down the mouse button while pressing Option and dragging, you move the root or stretch out the operation icon. Simply select the root, release the mouse button, and then press Option and drag out the datalink.

Of course, if you want to move *414*any of the items in the case window you can do so. Operation icons*732*, roots*736**961*, terminals*737**1071*, comments*730*, and even the input *486*733**and output*735* *814*bars can be moved *176*using the typical Macintosh click-and-drag. You can select more*415* than one item to *414**734*move at one time by holding down the Shift key while you select items. You can also drag a rectangular marquee across an area in the window and move all the items in the area at the same time. 
===========================================================

u Stretch the datalink a half inch or so below the ask operation and Option-Command-click. Doing this adds a new unnamed operation with a terminal connected to the root of the ask operation.

___________________________________________________________
NOTE: You can also use this technique to create a new operation with a root connected to the terminal of an existing operation.
-----------------------------------------------------------

u Type //birth and press Return. You have just provided birth with a context-determined method reference.*909* The “double slash” preceding the method name causes Prograph to look for the method in the same class as the method that contains the method-referencing operation. That is, Prograph looks for a birth method in the Person class, the class in which conception is defined. This is one of four forms of Prograph method reference.

___________________________________________________________
NOTE: The form for context-determined method reference  *192**696* is “//method” (“double slash”). This form of reference indicates that the named method is to be found in the same class as the current method that contains the method-referencing operation.

In explicit-class method reference, *362**703*the class name precedes the slash (/) character, followed by the method name. Prograph looks for the specified method in the specified class. In the current example, a Person/birth operation in conception produces the same result as //birth. In both forms, Prograph looks for the birth method in class Person. 

Data-determined method reference is a third type of method reference. Its form is “/method,” where no class name is specified before the single-slash character. The class of the object arriving on the leftmost terminal of the operation determines where Prograph looks for the specified method.

If a Person object were flowing into the birth-calling operation above, you could simply type /birth. Since different classes can each have a method called birth, data-determined method reference can be very powerful. The specific function of a method-calling operation can vary depending on what kind of object arrives on the terminal at runtime. In the current example, the data-determined /birth reference does not work, since the terminal is not receiving an object as input.

Universal method reference is the last type of method reference. Its form is “method”. No slash characters and no class reference are contained in a universal method reference. Prograph looks for the method in the pool of available Universal methods. A birth method reference would require the birth method to be defined as a universal method. While not an outright error, defining a Person-creating birth method as a universal method is at least illogical.

Sometimes more than one form of method reference produces the same result. Other times each form of reference can have profoundly different effects. As you proceed through the Grand Tour and the subsequent tutorials, you can develop an appreciation for and skill at using the various forms of method referencing.
-----------------------------------------------------------

The birth method you are about to create takes a character string as input, creates a new Person object, sets the object’s name attribute to the input string, and then returns the newborn object to conception. The user-defined method-operation icon already has the appropriate input terminal. Next, add the required *21*output root:

u Place the crosshair cursor just below the bottom edge of the birth operation and Command-click. *417*A new root appears at the click point.

u Click-drag in the conception case-window *141*title bar and position this window in the lower right corner of your screen (so it is not obscured when you open a birth method case window).

Before creating the birth method, add input terminal *24* and output root comments. *1088*These are carried into the new method to remind you which data flows into and out of this new method. Comments can be arbitrarily long and include many lines of text. However, here we are using comments as simple reminders.*170*

u Command-click the terminal of the //birth operation icon. A flashing text insertion bar appears just above the terminal. Type Name String.

u Command-click the root of the //birth operation icon. Type Newborn Person.

u Double-click while the crosshair is over the left side of the //birth operation (anywhere in the dark-filled portion of the operation, to the left of the operation name) to create the empty birth method in class Person. A case window for birth automatically opens, ready for you to specify its content. Note that the root on the input bar is annotated with your Name String comment, and the terminal of the output bar displays the Newborn Person comment.

===========================================================
TIP: Prograph operation icons have left and right click spots*792*. These locations are to the left and right sides of the icon, outside of the editable text field used to name the operation. These click spots are powerful features of the editor.
===========================================================
__________________________________________________________

The Prograph editor*892* is designed for user productivity. Online information describing Prograph primitives*785* is easy to access as you develop methods. And with Prograph’s dataflow specification, user-defined methods*1125* are developed intuitively. Here are some examples.

When you double-click on the left or right click spot of any Prograph primitive, the ˙Info… dialog pops up with the information for that primitive automatically displayed. Notes on inputs, outputs, and functions are at hand as you work. To try this feature now, double-click the left or right click spot of the ask operation in the conception method case window.

If you double-click the left click spot on operations which use explicit class or context determined method reference, the method’s case window opens so you can edit the dataflow cases of the method. If the method does not yet exist, an appropriately named blank method is installed in the appropriate class, and its case window is opened for you to create the new method. (*787*User-defined methods*725* with data-determined method referencing, “/method”, will pop up a dialog requesting identification of the class in which the method can be found or is to be created.)

This method cross-reference feature lets you develop user-defined methods interactively and intuitively. Suppose you begin developing a current method in which you refer to a method that has not yet been specified. You can leave the current method for a moment, pop over to specify the method you need to create in order for the current method to execute, and then pop back into the current method and continue. You can see how this works as you continue with the Grand Tour. You can find a comprehensive explanation of click spots in chapter 2, “The Editor Environment,” in the Reference manual.
---------------------------------------------------------

u About an inch below the input bar and to the left, Command-click to add an unnamed operation icon. Select the Instance item from the*647* *793* Opers (Operations) menu. The generic icon becomes an Instance generator operation icon.

u Type Person and press Return.

Instance generators, when executed, create an object of the named class. The newly “born” object exits the root of the instance generator.

The Person instance generator in birth creates a new Person object. The next goal of the birth method is to set the newborn Person object’s name attribute equal to the value of the name string that arrives on the root of the input bar. A Set attribute operation performs this task.

u Select the root of the Person instance generator, and then press the Option key as you move (without pressing the mouse button) the crosshair cursor down and slightly to the right. Option-Command-click to add an unspecified icon, and then select*658* *804*Set from the *978* Opers menu. (The birth case window was moved to the right in the following figure so you could see the Set menu selection and the birth method together.)

u Type name and press Return. The unspecified operation automatically becomes a syntactically correct*659* *805*Set operation, as follows:

The Set *979* operation takes an object input on its left terminal*1064* and a value*1141* on its right terminal. The input value on the right is used to set the value of the attribute designated by the operation identifier. In this example, name is the attribute being set. When executed, the object with its newly set attribute value exits at the root*954* of the Set operation to flow along to subsequent operations.

To set the name attribute, you first simply provide a datalink between the input bar root and the name set operation right-hand terminal.

u Click to select the right-hand terminal of the name set operation. Position the pointer directly over the “Name String” root of the input bar and press the Option key. A datalink stretches out from the name set terminal to the input bar root. Click when the arrow is right over the input-bar root: the datalink *252*will now be attached to the root.
___________________________________________________________
NOTE: Prograph does not let you connect roots to roots or terminals to terminals. It prevents you from making such dataflow-diagram errors.
-----------------------------------------------------------

=======================================================
*1096*TIP: To remove a datalink, simply repeat the datalink operation.
=======================================================

The final function of the birth method is to return the newborn Person object to conception.

u Select the root of the name set operation. Press Option as you position the pointer just above the output-bar terminal and click. A datalink is connected between the output of the name set operation and the output bar terminal. When the birth method is executed, the newborn, newly named Person object flows out the output bar.

This completes the birth method.

u Click the close box of the birth case window, and then click anywhere in the conception case window to activate it, if necessary.

u Select the root of the //birth operation, hold down the Option key to bring down a datalink from the root, and Option-Command-click to add an operation icon.

To have the new Person greet the world, create a greetings method in the Person class. To create this new method, you use another powerful feature of the Prograph editor.

u Type /greetings and press Return to name the newly placed operation icon. This*229* data-determined method reference*697* causes Prograph to search for greetings in the class of the object that flows into the operation at runtime. In this case, it is always a Person object coming from the //birth operation. 

___________________________________________________________
NOTE: If this example were not for instructional purposes, it would be better to use either the Person/greetings or //greetings method reference, as these allow more efficient processing by the Prograph interpreter.

With a /greetings reference, the interpreter does not know where to find the greetings method until it determines what type of object flows into the operation at runtime. By the definition of conception, the input object is always a Person coming from the birth operation. So the flexibility of the*230* data-determined method reference*698* is not really required here. It is just used to show you how it works.

Only a greetings method reference would be inappropriate in this example, because it would cause access to the universal greetings method you wrote earlier. Since the universal greetings method has no input-bar root, this call would generate an error.
-----------------------------------------------------------

u Double-click the left click spot of the /greetings operation. Since the editor cannot know what class of object will flow into the operation, a dialog appears.

u
Since you know the incoming object is a Person instance, click Person in the scroll list of the Classes without method: Select to create & open method. dialog.*163*

u
Click Select to create this new method and open its case window.

___________________________________________________________
NOTE: The greetings method in class Person is created with a root on its input bar reflecting the terminal on the /greetings user-defined operation icon in conception. When you double-click on a user-defined operation in one method to create a second, new method, Prograph’s editor performs an “arity check.” *79* This means Prograph ensures there is a one-to-one relationship between roots and terminals on the method-referencing operation, and roots on the input bar and terminals on the output bar in the case window of the newly created method. Notice that the comments added to the //birth operation in conception now appear on the input bar of the /greetings case window.
-----------------------------------------------------------

To continue specifying greetings in the Person class, you first need to pull the character string from the name attribute of the incoming Person object. You then join that string with a greeting string. The greeting string is a constant value *190*similar to what you used in the universal greetings method. The resulting string to be displayed with the show *847**986*operation is a salutation that includes an identification of the speaker.

u Select the root of the input bar and Option-Command-click a new operation slightly below the input root.

u Select*648* *794*Get from the Opers menu*389*, type name, and press Return.

Getting an attribute is a mirror image to setting an attribute. The Get operation does two things. It takes the input Person object, passes the person through on the left output root, and sends the data value (the person’s name) out the right root. In this method, you are only interested in the character string from the name attribute, so the left root is left unconnected.

___________________________________________________________
NOTE: Unconnected roots*963* do not cause errors. If an operation returns a data value or object that is not required, just ignore it. The value or object arrives at the unconnected root and stops there.
-----------------------------------------------------------

u Select the right root of the name get operation and Option-Command-click a new operation slightly below the name get operation.

u
Select*850* Primitives from the ˙Info… dialog, find the "join" primitive (its name includes the two quotes) and click on it to select it. This primitive takes two or more inputs and returns a single string output. The strings from the input terminals are joined left to right.

u
Select Name, and then press Return. The "join" operation icon takes shape with two terminals and one root. The datalink from the name get operation is connected to the left terminal.

Although "join" is initially specified with two input terminals, three strings must be joined to have the greetings method produce the following dialog output: "Hello, World! My name is Johnny." The leftmost input string has "Hello, World! My name is " value. The second input is the name coming in from the Get operation. The third input string value is a simple period character to punctuate the greeting.

u
To add *25* the third input terminal*1067* to carry the first part of the greeting, point just above the top edge of the "join" operation icon and to the left of the terminal connected to the name get datalink, Command-click, and double-click.

u
Type Hello, World! My name is with a space at the end and press Return. The resulting constant operation*731* might crowd the name get icon, *191*so drag the operation to the left (or Command-drag if you have difficulty finding a spot to grab anywhere on the constant). The datalink stretches to accommodate the new position.

u
Double-click the right-side terminal of "join" to add another constant and type a period character. Then press Return.

This completes input of the strings to be joined for the greeting.

u
Select the root of "join" and Option-Command-click a new operation. Type show and press Return.

Your complete greetings method now looks like the following:

To see a Person in action, execute conception.

u
Click on the Person class Methods window, and then click on the conception method icon (you may first need to do a Tidy Icons from the Windows menu in case you have a method icon hiding behind another). Alternatively, click on the conception case window to make it the topmost, active window. Either approach selects a method for execution.

u
Select*618* Execute Method *337*from the Exec menu *349*or press Command-E.

The prompt dialog asks for the name of the new person.

u
Type Johnny and press Return.

In an instant, conception passes the name to birth. Johnny is born and returned to conception, where he is passed to greetings to proclaim the following:

Congratulations! You have elaborated your Person class to include three new methods that work together. As long as you or your end user interact with these methods the way you intend, there is no problem. But users are notoriously independent and particularly skilled at making things go wrong. The Prograph development environment is well suited for contending with the inevitable bugs.